<?php


namespace addons\workteam\library\notice\queue;

use app\admin\model\Admin;
use app\common\library\Email;
use think\Log;
use think\Queue;
use think\queue\job;
use think\Validate;

/**
 * 项目相关
 * Class ProjectJob
 * @package addons\workteam\library\notice\queue
 */
class ProjectJob
{
    public function fire(Job $job, $data)
    {


        if ($job->attempts() > 3) {
            //通过这个方法可以检查这个任务已经重试了几次了
            $job->delete();
            return;
        }
        if (!isset($data['key'])) {
            $job->delete();
            return;
        }
        $key = $data['key'];

        $notice_types = \addons\workteam\library\notice\Notice::getProviders();
        $keys = "";
        foreach ($notice_types as $v) {
            $keys .= $keys ? ',' . $key . $v : $key . $v;
        }


        $settingModel = new \app\admin\model\workteam\Setting();
        $setinglist = $settingModel->where('key', 'in', $keys)->where('status', 1)->select();

        if (!$setinglist) {
            $job->delete();
            return false;
        }
        //字段处理
        foreach ($data as $k => &$v) {
            if (strpos($k, '_time') !== false) {
                $v = datetime($v);
            }
        }

        foreach ($setinglist as $r) {
            if (!$r->values['engine']) continue;
            $notice_app = new \addons\workteam\library\notice\Notice(['key' => $r->values['engine']]);
            $notice_app->send($r, $data, $data);
        }

		$job->delete();
        return true;


    }

    public function failed($data)
    {
        Log::write("项目相关通知失败：" . print_r(['data' => $data,], true), 'error');
    }

}